/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "kerneldebugconsole.h"

#include <machine/machine.h>

PLKernelDebugConsole::PLKernelDebugConsole() {
	m_x = 0;
	m_y = 10;
	m_width = COLUMNS;
	m_height = 15;
	m_debugActive = PLMachine::instance().kernelCommandline()->kernelDebug();
}

void PLKernelDebugConsole::cleared() {
	if(m_debugActive) {
		for(int j = m_x ; j < m_width + m_x ; j++) {
			*(m_video + (((m_y - 1) * COLUMNS) + j) * 2)     = 177 & 0xFF;
			*(m_video + (((m_y - 1) * COLUMNS) + j) * 2 + 1) = PLColorVGA16::red;
		}
	}
}

void PLKernelDebugConsole::putchar(int c, PLColorVGA16 attr) {
	if(m_debugActive) {
		if (c == '\n' || c == '\r') {
			newlineScroll();
			return;
		}
		*(m_video + (m_xpos + (LINES - m_ypos) * COLUMNS) * 2) = c & 0xFF;
		*(m_video + (m_xpos + (LINES - m_ypos) * COLUMNS) * 2 + 1) = attr;
		m_xpos++;
		if(m_xpos >= COLUMNS) {
			newlineScroll();
		}
	}
}

void PLKernelDebugConsole::newlineScroll() {
	int i;
	int j;
	for(i = m_y + 1 ; i < m_height + m_y ; i++) {
		for(j = m_x ; j < m_width + m_x ; j++) {
			*(m_video + (((i - 1) * COLUMNS) + j) * 2)     = *(m_video + ((i * COLUMNS) + j) * 2);
			*(m_video + (((i - 1) * COLUMNS) + j) * 2 + 1) = *(m_video + ((i * COLUMNS) + j) * 2 + 1);
		}
	}
	i--;
	for(j = m_x ; j < m_width + m_x ; j++) {
		*(m_video + ((i * COLUMNS) + j) * 2)     = ' ';
		*(m_video + ((i * COLUMNS) + j) * 2 + 1) = PLColorVGA16::black;
	}
	m_xpos = 0;
	m_ypos = 0;
}
